import Cache from "@/utils/cache";
import {getUserinfo} from "@/http/api/user/user.js"
import { getLoginTutorInfo } from "@/http/api/tutor/tutor.js" 
import {getCategoryList} from "@/http/api/category/category.js" 
// #ifndef VUE3
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

const store = new Vuex.Store({
// #endif

// #ifdef VUE3
import {
   createStore
} from 'vuex'
const store = createStore({
   // #endif
   state: {
	   //登录用户的信息
	   userInfo:JSON.parse(Cache.get('userInfo')||JSON.stringify('')),
	   userShopDetails:{},//当前登录店铺信息
	   userTutorDetails:{},//当前登录教练信息
	   categoryList:[],//分类列表
	   isMerchant:'',//0是商家 1不是
	   isAssistant:'',//0是教练 1不是
	   addressInfo:{
		   latitude: '', // 纬度
		   longitude: '', // 经度
		   name: '', // 名称
		   province: '', // 省份
		   city: '', // 市区
		   district: '', // 区域
		   businessAreaName: '', // 商圈
	   },//选择的定位数据 
	   userId:null,//登录用户id
	   //登录后跳转的页面路径 + 页面参数
	   returnUrl: "",
	   isOnline: true,
	   whichEnd: 'tutor', // index 用户端   shop 商户端  tutor 教练端
	   tutorTabBar: [{
			   "pagePath": "/pages/invite/invite",
			   "text": "邀约",
			   "iconPath": "/static/tabbar/tutor/yaoyue.png",
			   "selectedIconPath": "/static/tabbar/tutor/yaoyue_active.png",
		   },
		   {
			   "pagePath": "/pages/order/order",
			   "text": "订单",
			   "iconPath": "/static/tabbar/tutor/order.png",
			   "selectedIconPath": "/static/tabbar/tutor/order_active.png"
		   },
		   {
			   "pagePath": "/pages/dynamic/dynamic",
			   "text": "动态",
			   "iconPath": "/static/tabbar/tutor/WechatIMG1865.jpg",
			   "selectedIconPath": "/static/tabbar/tutor/WechatIMG1866.jpg"
		   },
		   // {
			  //  "pagePath": "/pages/personal-details/personal-details",
			  //  "text": "个人信息",
			  //  "iconPath": "/static/tabbar/tutor/message.png",
			  //  "selectedIconPath": "/static/tabbar/tutor/message_active.png",
		   // },
		   {
			   "pagePath": "/pages/my/my",
			   "text": "我的",
			   "iconPath": "/static/tabbar/tutor/mine.png",
			   "selectedIconPath": "/static/tabbar/tutor/mine_active.png",
		   }
	   ],
	   tabBarIndex: 0
   },
   mutations: {  
	   setMerchant(state, val) {
			   state.isMerchant = val;
	   },
	   setUserShopDetails(state, val){
		   state.userShopDetails = val;
	   },
	   //设置当前登录教练端信息 
	   setUserTutorDetails(state, val){
		   state.userTutorDetails = val;
	   },
	   //设置分类列表
	   setCategoryList(state, val){
		   state.categoryList = val;
	   },
	   clearAdressInfo(state, val){
		   state.addressInfo = {
			   latitude: '', // 纬度
			   longitude: '', // 经度
			   name: '', // 名称
			   province: '', // 省份
			   city: '', // 市区
			   district: '', // 区域
			   businessAreaName: '', // 商圈
		   }
	   },
	   setAssistant(state, val) {
			   state.isAssistant = val;
	   },
	   setAdressInfo(state, val){
		   state.addressInfo = val;
	   },
	   setUserInfo(state, val) {
			   state.userInfo = val;
	   },
	   changeTabItem(state, item) {
		   if (item) {
			   state.whichEnd = item
		   }
	   },
	   setTabBarIndex(state, payload) {
		   state.tabBarIndex = payload.index
	   },
	   changeTabBar(state, payload) { 
		   console.log(payload,'点击tabber')
		   state.tabBarIndex = payload.index
	   },
	   login(state, payload) {
		   if (payload) {
			   state.mobile = payload.mobile
		   }
		   state.isLogin = true
	   },
	   logout(state) {
		   state.mobile = ""
		   state.isLogin = false
		   state.returnUrl = ""
	   },
	   setReturnUrl(state, returnUrl) {
		   state.returnUrl = returnUrl
	   },
	   networkChange(state, payload) {
		   state.networkConnected = payload.isConnected
	   },
	   setOnline(state, payload) {
		   state.isOnline = state.isOnline
	   }
   },
   getters: {
	   // 根据 whichEnd 动态选择数据
	   tabBar: (state, getters) => {
		   let selectedTabBar = [];
		   // 计算并返回tabbar数据
		   return state.tutorTabBar
	   },
	   // 根据 id 返回分类name
	   categoryName: (state) => (id) => {
		   const item = state.categoryList.find((item) => item.id === id);
		   return item ? item.categoryName : null; // 如果找不到，返回 null
	 },
   },
   actions: {
		   getUserInfo(store) {
				   getLoginTutorInfo().then((res) => {
					   if (res.code == 200) {
						   store.commit("setUserTutorDetails", res.data); 
						   Cache.set('userId',res.data&&res.data.id) 
						   let info=JSON.stringify(res.data)
						   Cache.set('userInfo',info) 
					   } else {
						   store.commit("setUserInfo", false);
					   }
				   });
			   }, 
		   getCategorys(store) { 
			   getCategoryList().then((res) => {
				   if (res.code == 200) {
					   store.commit("setCategoryList", res.data); 
				   } else {
					   store.commit("setUserInfo", false);
				   }
			   });
		   },
   }
})

export default store